<?php

namespace App\Containers\AppSection\User\Tasks;

use App\Containers\AppSection\User\Data\Criterias\RoleCriteria;
use App\Containers\AppSection\User\Data\Repositories\UserRepository;
use App\Ship\Parents\Tasks\Task;

class GetAllUsersTask extends Task
{
    protected UserRepository $repository;

    public function __construct(UserRepository $repository)
    {
        $this->repository = $repository;
    }

    public function run()
    {
        return $this->repository->paginate();
    }

    public function withRole($role = null): self
    {
        if (!empty($role)) {
            $this->repository->pushCriteria(new RoleCriteria($role));
        }
        return $this;
    }

    public function withRoleType($role = null): self
    {
        if (!empty($role)) {
            $this->repository->whereHas('roles', function ($q) use ($role) {
                $q->where('role_type', $role);
            });
        }
        return $this;
    }

    public function with(string...$with): self
    {
        $this->repository->with([...$with]);
        return $this;
    }
}
